<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <meta name="google-site-verification" content="k9iQUbEI9rWq3xYeh63ATztKdkthC4dNRHV_25maJ3Q" />
  <title>laravel学习笔记-用户认证 | Taylor&#39;s Learning Diary</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="在平时开发过程中，用户登录注册是非常常见的场景，比如大部分后台管理、论坛等等；用户一般web访问流程从业务来说是这样的：">
<meta name="keywords" content="laravel">
<meta property="og:type" content="article">
<meta property="og:title" content="laravel学习笔记-用户认证">
<meta property="og:url" content="https://upeng.github.io/blog/2016/05/29/laravel-auth/index.html">
<meta property="og:site_name" content="Taylor&#39;s Learning Diary">
<meta property="og:description" content="在平时开发过程中，用户登录注册是非常常见的场景，比如大部分后台管理、论坛等等；用户一般web访问流程从业务来说是这样的：">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2017-09-28T15:11:28.206Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="laravel学习笔记-用户认证">
<meta name="twitter:description" content="在平时开发过程中，用户登录注册是非常常见的场景，比如大部分后台管理、论坛等等；用户一般web访问流程从业务来说是这样的：">
  
    <link rel="alternate" href="/atom.xml" title="Taylor&#39;s Learning Diary" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/blog/css/style.css">
  

</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog/" id="logo">Taylor&#39;s Learning Diary</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/blog/">Home</a>
        
          <a class="main-nav-link" href="/blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://upeng.github.io/blog"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-laravel-auth" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/blog/2016/05/29/laravel-auth/" class="article-date">
  <time datetime="2016-05-29T00:17:07.000Z" itemprop="datePublished">2016-05-29</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/blog/categories/PHP/">PHP</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      laravel学习笔记-用户认证
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>在平时开发过程中，用户登录注册是非常常见的场景，比如大部分后台管理、论坛等等；用户一般web访问流程从业务来说是这样的：<br><a id="more"></a></p>
<ol>
<li>注册</li>
<li>登录</li>
<li>进入首页</li>
<li>查看&amp;操作</li>
<li>退出登录</li>
</ol>
<p>很多时候为了实现1、2、5这部分功能，重复造了很多轮子。目前接触到的laravel，就避免了你重复造轮子的问题，下面结合自己在laravelacademy上学习的laravel认证这部分做一下记录和总结，我学习的思路大致上是先学会用，然后搞清楚原理；所以这篇blog先记录如何使用的，快速实现function，尽管写的很low，但是给自己看，最关键的是是一个温习的过程，fighting！</p>
<p>laravel认证【插件】做了什么？</p>
<ul>
<li>注册</li>
<li>登录</li>
<li>找回密码（这都帮忙做好了^_^）</li>
</ul>
<h3 id="数据库表准备"><a href="#数据库表准备" class="headerlink" title="数据库表准备"></a>数据库表准备</h3><p>注册其实是入库的过程，也就是说，你需要有一个表专门用以存储注册用户的信息，laravel的<code>migration</code>已经自动创建了<code>users</code>和<code>password_resets</code>建表语句，只需<code>php artisan migrate</code>即可创建2张表到数据库；况且Model层已经创建了<code>User.php</code><br><!-- more --></p>
<h3 id="路由设置"><a href="#路由设置" class="headerlink" title="路由设置"></a>路由设置</h3><figure class="highlight clean"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 认证路由...</span></div><div class="line">Route::get(<span class="string">'auth/login'</span>, <span class="string">'Auth\AuthController@getLogin'</span>);</div><div class="line">Route::post(<span class="string">'auth/login'</span>, <span class="string">'Auth\AuthController@postLogin'</span>);</div><div class="line">Route::get(<span class="string">'auth/logout'</span>, <span class="string">'Auth\AuthController@getLogout'</span>);</div><div class="line"></div><div class="line"><span class="comment">// 注册路由...</span></div><div class="line">Route::get(<span class="string">'auth/register'</span>, <span class="string">'Auth\AuthController@getRegister'</span>);</div><div class="line">Route::post(<span class="string">'auth/register'</span>, <span class="string">'Auth\AuthController@postRegister'</span>);</div><div class="line"></div><div class="line"><span class="comment">// 发送密码重置链接路由</span></div><div class="line">Route::get(<span class="string">'password/email'</span>, <span class="string">'Auth\PasswordController@getEmail'</span>);</div><div class="line">Route::post(<span class="string">'password/email'</span>, <span class="string">'Auth\PasswordController@postEmail'</span>);</div><div class="line"></div><div class="line"><span class="comment">// 密码重置路由</span></div><div class="line">Route::get(<span class="string">'password/reset/&#123;token&#125;'</span>, <span class="string">'Auth\PasswordController@getReset'</span>);</div><div class="line">Route::post(<span class="string">'password/reset'</span>, <span class="string">'Auth\PasswordController@postReset'</span>);</div><div class="line"></div><div class="line"><span class="comment">//成功认证后网站首页</span></div><div class="line">Route::get(<span class="string">'/'</span>, <span class="string">'UserController@index'</span>);</div></pre></td></tr></table></figure>
<p>你会发现<code>AuthController</code>没有<code>getLogin、postLogin</code>这些方法，这里需要说明一下<a href="http://php.net/manual/zh/language.oop5.traits.php" target="_blank" rel="external">trait</a>官方手册讲述的已经很清楚，顺便学习一下overtrue的这篇文章<a href="http://overtrue.me/articles/2016/04/about-php-trait.html" target="_blank" rel="external">我所理解的 PHP Trait</a>，所以如果想知道auth怎么操作数据库，又是如何加载视图，做何种跳转，深入trait层去看代码就好。</p>
<h3 id="控制器"><a href="#控制器" class="headerlink" title="控制器"></a>控制器</h3><p>laravel已经创建好了auth控制器，位于<code>Controllers/Auth/</code>下，而且中间件也都默认创建，基本的准备工作已经做足，只需你创建view，做一些简单的设置就可以使用了</p>
<h4 id="部分属性设置"><a href="#部分属性设置" class="headerlink" title="部分属性设置"></a>部分属性设置</h4><p>注册登录和密码重置的路由入口都在<code>Controllers/Auth/</code>下的<code>AuthController.php</code>和<code>PasswordController.php</code>,他们都是用了<code>trait</code><br>如果你在控制器中没有定义一些path，<code>trait</code>底层都会使用自己定义的默认值，如果你喜欢这个默认值，你就按照他来写你的<code>route.php</code>吧，这里我们可以自己定义，覆盖<code>trait</code>中的这部分属性；如果不知道属性名称，进入<code>trait</code>查看</p>
<blockquote>
<p>eg:<br><figure class="highlight lasso"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//in AuthController.php</span></div><div class="line"><span class="keyword">protected</span> $redirectPath = <span class="string">'/'</span>;  <span class="comment">//登录或注册成功进入首页，默认是/home</span></div><div class="line"><span class="keyword">protected</span> $redirectAfterLogout = <span class="string">'/auth/login'</span>; <span class="comment">//退出登录后跳转到登录页</span></div><div class="line"></div><div class="line"><span class="comment">//in PasswordController.php</span></div><div class="line"><span class="keyword">protected</span> $redirectPath = <span class="string">'/'</span>;  <span class="comment">//密码重置成功后跳转</span></div><div class="line"><span class="keyword">protected</span> $subject = <span class="string">'点击此处重置密码：'</span>;  <span class="comment">//邮件内容</span></div></pre></td></tr></table></figure></p>
</blockquote>
<h4 id="发挥认证作用"><a href="#发挥认证作用" class="headerlink" title="发挥认证作用"></a>发挥认证作用</h4><p>比如在登录成功后跳转的路由’/‘，我们在<code>UserController</code>中怎么确定用户是否登录，如果没有登录怎么让用户成功登录后callback到其输入的地址；<strong>auth中间件开始发挥其作用了</strong>，所谓中间件，从字面上理解就是在用户访问该页面前，先去执行中间件内容。其实在Auth中也使用了中间件guest;在UserController中加入下面这个构造函数，即可让用户先登录再访问。<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="php"><span class="meta">&lt;?php</span></span></div><div class="line"><span class="php"></span></div><div class="line"><span class="php"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Controllers</span>;</span></div><div class="line"><span class="php"></span></div><div class="line"><span class="php"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Http</span>\<span class="title">Request</span>;</span></div><div class="line"><span class="php"></span></div><div class="line"><span class="php"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Requests</span>;</span></div><div class="line"><span class="php"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Controllers</span>\<span class="title">Controller</span>;</span></div><div class="line"><span class="php"></span></div><div class="line"><span class="php"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span></div><div class="line"><span class="php">&#123;</span></div><div class="line"><span class="php">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">()</span></span></span></div><div class="line"><span class="php">    &#123;</span></div><div class="line"><span class="php">        <span class="keyword">$this</span>-&gt;middleware(<span class="string">'auth'</span>);</span></div><div class="line"><span class="php">    &#125;</span></div><div class="line">    /**</div><div class="line">     * Display a listing of the resource.</div><div class="line">     *</div><div class="line">     * @return \Illuminate\Http\Response</div><div class="line"><span class="php">     */</span></div><div class="line"><span class="php">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">index</span><span class="params">(Request $request)</span></span></span></div><div class="line"><span class="php">    &#123;</span></div><div class="line"><span class="php">        $user = $request-&gt;user();</span></div><div class="line"><span class="php">        <span class="keyword">echo</span> $user[<span class="string">'name'</span>] . <span class="string">'登录成功！'</span>;</span></div><div class="line"><span class="php">    &#125;</span></div><div class="line"><span class="php">&#125;</span></div></pre></td></tr></table></figure></p>
<h3 id="发散"><a href="#发散" class="headerlink" title="发散"></a>发散</h3><h4 id="关于模板"><a href="#关于模板" class="headerlink" title="关于模板"></a>关于模板</h4><p>使用Form表单类，加上bootstrap打造属于自己的登录注册界面，模板文件名源于trait中定义的view，当然我们可以重写getLogin等方法，因为它们有的简单到只有一个<code>return veiw(*.blade.php)</code><br>主要创建以下模板：<a href="https://github.com/upeng/learn-laravel" target="_blank" rel="external">详细页面</a></p>
<ul>
<li>register.blade.php</li>
<li>login.blade.php</li>
<li>password.blade.php</li>
<li>reset.blade.php</li>
</ul>
<h4 id="关于设置session"><a href="#关于设置session" class="headerlink" title="关于设置session"></a>关于设置session</h4><p>我们常见的登录登出按钮，重置密码，获取当前登录用户的基本信息等等。开始搞起吧！<br>后面仔细研究源码，从根本上掌握auth认真的全流程。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://upeng.github.io/blog/2016/05/29/laravel-auth/" data-id="cj8x7816d000bgwp4th805mgl" class="article-share-link">Share</a>
      
        <a href="https://upeng.github.io/blog/2016/05/29/laravel-auth/#disqus_thread" class="article-comment-link">留言</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/laravel/">laravel</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog/2016/05/29/thinking/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          standing on the shoulders of the giants
        
      </div>
    </a>
  
  
    <a href="/blog/2016/05/28/php-detail/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">几个值得深思的 PHP 面试问题</div>
    </a>
  
</nav>

  
</article>


<section id="comments">
  <div id="disqus_thread">
    <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
  </div>
</section>
</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Linux/">Linux</a><span class="category-list-count">8</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/MySQL/">MySQL</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/PHP/">PHP</a><span class="category-list-count">15</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Thinking/">Thinking</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/其他/">其他</a><span class="category-list-count">8</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/前端/">前端</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/数据库范式/">数据库范式</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/blog/tags/CI/" style="font-size: 10px;">CI</a> <a href="/blog/tags/JQuery/" style="font-size: 10px;">JQuery</a> <a href="/blog/tags/Mac/" style="font-size: 13.33px;">Mac</a> <a href="/blog/tags/MySQL/" style="font-size: 10px;">MySQL</a> <a href="/blog/tags/awk/" style="font-size: 10px;">awk</a> <a href="/blog/tags/bash/" style="font-size: 10px;">bash</a> <a href="/blog/tags/composer/" style="font-size: 10px;">composer</a> <a href="/blog/tags/eloquent/" style="font-size: 10px;">eloquent</a> <a href="/blog/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/blog/tags/idempotence/" style="font-size: 10px;">idempotence</a> <a href="/blog/tags/item/" style="font-size: 10px;">item</a> <a href="/blog/tags/laravel/" style="font-size: 20px;">laravel</a> <a href="/blog/tags/linux/" style="font-size: 16.67px;">linux</a> <a href="/blog/tags/mac/" style="font-size: 10px;">mac</a> <a href="/blog/tags/memcacheq/" style="font-size: 10px;">memcacheq</a> <a href="/blog/tags/mysql/" style="font-size: 16.67px;">mysql</a> <a href="/blog/tags/nc/" style="font-size: 10px;">nc</a> <a href="/blog/tags/packageist/" style="font-size: 10px;">packageist</a> <a href="/blog/tags/php/" style="font-size: 13.33px;">php</a> <a href="/blog/tags/sed/" style="font-size: 10px;">sed</a> <a href="/blog/tags/shell/" style="font-size: 10px;">shell</a> <a href="/blog/tags/static/" style="font-size: 10px;">static</a> <a href="/blog/tags/thinking/" style="font-size: 13.33px;">thinking</a> <a href="/blog/tags/tmux/" style="font-size: 10px;">tmux</a> <a href="/blog/tags/vagrant/" style="font-size: 10px;">vagrant</a> <a href="/blog/tags/validator/" style="font-size: 10px;">validator</a> <a href="/blog/tags/vim/" style="font-size: 10px;">vim</a> <a href="/blog/tags/vuejs/" style="font-size: 10px;">vuejs</a> <a href="/blog/tags/zephir/" style="font-size: 10px;">zephir</a> <a href="/blog/tags/zsh/" style="font-size: 10px;">zsh</a> <a href="/blog/tags/设计模式/" style="font-size: 13.33px;">设计模式</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2017/10/">十月 2017</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2017/09/">九月 2017</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/12/">十二月 2016</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/11/">十一月 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/10/">十月 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/09/">九月 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/08/">八月 2016</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/07/">七月 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/06/">六月 2016</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/05/">五月 2016</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/04/">四月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/02/">二月 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/01/">一月 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2015/10/">十月 2015</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2015/08/">八月 2015</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2015/07/">七月 2015</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog/2017/10/18/laravel-validator/">Laravel Validator自定义参数验证规则</a>
          </li>
        
          <li>
            <a href="/blog/2017/09/28/linux-sed/">linux常用命令之sed</a>
          </li>
        
          <li>
            <a href="/blog/2017/09/27/shell-script-learning/">shell脚本由点到面学习总结</a>
          </li>
        
          <li>
            <a href="/blog/2017/09/14/laravel-eloquent-index/">Eloquent ORM多个and和or条件查询</a>
          </li>
        
          <li>
            <a href="/blog/2017/09/10/Linux压缩解压缩命令-index/">Linux常用压缩解压缩命令</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2017 Tayloryu<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/blog/" class="mobile-nav-link">Home</a>
  
    <a href="/blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    
<script>
  var disqus_shortname = 'tayloryu';
  
  var disqus_url = 'https://upeng.github.io/blog/2016/05/29/laravel-auth/';
  
  (function(){
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>


<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.css">
  <script src="/blog/fancybox/jquery.fancybox.pack.js"></script>


<script src="/blog/js/script.js"></script>
  </div>
</body>
</html>